package com.ricky.service_hospital.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.ricky.hosp.cmn_client.DictFeignClient;
import com.ricky.hosp.model.hosp.Hospital;
import com.ricky.hosp.vo.hosp.HospitalQueryVo;
import com.ricky.service_hospital.repository.HospitalRepository;
import com.ricky.service_hospital.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //将参数的map几何转换成对象Hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //先判断mongodb中是否存在相同的数据，存在则更新，不存在则添加
        String hoscode = hospital.getHoscode();
        Hospital hosExist = hospitalRepository.getHospitalByHoscode(hoscode);
        //如果存在，则进行修改
        if (hosExist!=null){
            hospital.setStatus(hosExist.getStatus());
            hospital.setCreateTime(hosExist.getCreateTime());
            hospital.setUpdateTime(hosExist.getUpdateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            //如果不存在，则进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //议员列表（条件查询带分页）
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建Example对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        hospital.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        //获取list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(this::setHospitalHosType);
        return pages;
    }
    //更新医院状态
    @Override
    public void updateStates(String id, Integer status) {
        Optional<Hospital> hospitalOptional = hospitalRepository.findById(id);
        if (hospitalOptional.isPresent()){
            Hospital hospital = hospitalOptional.get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }
    // 获取医院详情
    @Override
    public Map<String,Object> getHospById(String id) {
        HashMap<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        result.put("hospital", hospital);
        result.put("bookingRule", hospital.getBookingRule());
        return result;
    }
    //根据医院名称模糊查询
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        HashMap<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital",hospital);
        //医院规则
        result.put("bookingRule",hospital.getBookingRule());
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital.getHosname();
    }

    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市 地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        return hospital;
    }
}
